1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 package java.awt;
26
27 import java.awt.event.FocusEvent;
28 import java.awt.event.InputEvent;
29 import java.awt.event.KeyEvent;
30 import java.awt.event.WindowEvent;
31
32 import java.awt.peer.KeyboardFocusManagerPeer;
33 import java.awt.peer.LightweightPeer;
34
35 import java.beans.PropertyChangeListener;
36 import java.beans.PropertyChangeSupport;
37 import java.beans.PropertyVetoException;
38 import java.beans.VetoableChangeListener;
39 import java.beans.VetoableChangeSupport;
40
41 import java.lang.ref.WeakReference;
42
43 import java.lang.reflect.Field;
44
45 import java.security.AccessController;
46 import java.security.PrivilegedAction;
47
48 import java.util.Collections;
49 import java.util.HashSet;
50 import java.util.Iterator;
51 import java.util.LinkedList;
52 import java.util.Set;
53 import java.util.StringTokenizer;
54 import java.util.WeakHashMap;
55
56 import sun.util.logging.PlatformLogger;
57
58 import sun.awt.AppContext;
59 import sun.awt.HeadlessToolkit;
60 import sun.awt.SunToolkit;
61 import sun.awt.CausedFocusEvent;
62 import sun.awt.KeyboardFocusManagerPeerProvider;
63 import sun.awt.AWTAccessor;
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public abstract class KeyboardFocusManager
109 implements KeyEventDispatcher, KeyEventPostProcessor
110 {
111
112
113 private static final PlatformLogger focusLog = PlatformLogger.getLogger("java.awt.focus.KeyboardFocusManager");
114
115 static {
116
117 Toolkit.loadLibraries();
118 if (!GraphicsEnvironment.isHeadless()) {
119 initIDs();
120 }
121 AWTAccessor.setKeyboardFocusManagerAccessor(
122 new AWTAccessor.KeyboardFocusManagerAccessor() {
123 public int shouldNativelyFocusHeavyweight(Component heavyweight,
124 Component descendant,
125 boolean temporary,
126 boolean focusedWindowChangeAllowed,
127 long time,
128 CausedFocusEvent.Cause cause)
129 {
130 return KeyboardFocusManager.shouldNativelyFocusHeavyweight(
131 heavyweight, descendant, temporary, focusedWindowChangeAllowed, time, cause);
132 }
133 public boolean processSynchronousLightweightTransfer(Component heavyweight,
134 Component descendant,
135 boolean temporary,
136 boolean focusedWindowChangeAllowed,
137 long time)
138 {
139 return KeyboardFocusManager.processSynchronousLightweightTransfer(
140 heavyweight, descendant, temporary, focusedWindowChangeAllowed, time);
141 }
142 public void removeLastFocusRequest(Component heavyweight) {
143 KeyboardFocusManager.removeLastFocusRequest(heavyweight);
144 }
145 public void setMostRecentFocusOwner(Window window, Component component) {
146 KeyboardFocusManager.setMostRecentFocusOwner(window, component);
147 }
148 }
149 );
150 }
151
152 transient KeyboardFocusManagerPeer peer;
153
154
155
156
157 private static native void initIDs();
158
159 private static final PlatformLogger log = PlatformLogger.getLogger("java.awt.KeyboardFocusManager");
160
161
162
163
164
165
166
167
168
169 public static final int FORWARD_TRAVERSAL_KEYS = 0;
170
171
172
173
174
175
176
177
178
179 public static final int BACKWARD_TRAVERSAL_KEYS = 1;
180
181
182
183
184
185
186
187
188
189 public static final int UP_CYCLE_TRAVERSAL_KEYS = 2;
190
191
192
193
194
195
196
197
198
199 public static final int DOWN_CYCLE_TRAVERSAL_KEYS = 3;
200
201 static final int TRAVERSAL_KEY_LENGTH = DOWN_CYCLE_TRAVERSAL_KEYS + 1;
202
203
204
205
206
207
208
209
210 public static KeyboardFocusManager getCurrentKeyboardFocusManager() {
211 return getCurrentKeyboardFocusManager(AppContext.getAppContext());
212 }
213
214 synchronized static KeyboardFocusManager
215 getCurrentKeyboardFocusManager(AppContext appcontext)
216 {
217 KeyboardFocusManager manager = (KeyboardFocusManager)
218 appcontext.get(KeyboardFocusManager.class);
219 if (manager == null) {
220 manager = new DefaultKeyboardFocusManager();
221 appcontext.put(KeyboardFocusManager.class, manager);
222 }
223 return manager;
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243 public static void setCurrentKeyboardFocusManager(
244 KeyboardFocusManager newManager) throws SecurityException
245 {
246 SecurityManager security = System.getSecurityManager();
247 if (security != null) {
248 if (replaceKeyboardFocusManagerPermission == null) {
249 replaceKeyboardFocusManagerPermission =
250 new AWTPermission("replaceKeyboardFocusManager");
251 }
252 security.
253 checkPermission(replaceKeyboardFocusManagerPermission);
254 }
255
256 KeyboardFocusManager oldManager = null;
257
258 synchronized (KeyboardFocusManager.class) {
259 AppContext appcontext = AppContext.getAppContext();
260
261 if (newManager != null) {
262 oldManager = getCurrentKeyboardFocusManager(appcontext);
263
264 appcontext.put(KeyboardFocusManager.class, newManager);
265 } else {
266 oldManager = getCurrentKeyboardFocusManager(appcontext);
267 appcontext.remove(KeyboardFocusManager.class);
268 }
269 }
270
271 if (oldManager != null) {
272 oldManager.firePropertyChange("managingFocus",
273 Boolean.TRUE,
274 Boolean.FALSE);
275 }
276 if (newManager != null) {
277 newManager.firePropertyChange("managingFocus",
278 Boolean.FALSE,
279 Boolean.TRUE);
280 }
281 }
282
283
284
285
286
287 private static Component focusOwner;
288
289
290
291
292
293
294 private static Component permanentFocusOwner;
295
296
297
298
299 private static Window focusedWindow;
300
301
302
303
304
305
306
307
308 private static Window activeWindow;
309
310
311
312
313
314
315
316
317 private FocusTraversalPolicy defaultPolicy =
318 new DefaultFocusTraversalPolicy();
319
320
321
322
323 private static final String[] defaultFocusTraversalKeyPropertyNames = {
324 "forwardDefaultFocusTraversalKeys",
325 "backwardDefaultFocusTraversalKeys",
326 "upCycleDefaultFocusTraversalKeys",
327 "downCycleDefaultFocusTraversalKeys"
328 };
329
330
331
332
333 private static final AWTKeyStroke[][] defaultFocusTraversalKeyStrokes = {
334 {
335 AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, 0, false),
336 AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK, false),
337 },
338 {
339 AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB, InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK, false),
340 AWTKeyStroke.getAWTKeyStroke(KeyEvent.VK_TAB,
341 InputEvent.SHIFT_DOWN_MASK | InputEvent.SHIFT_MASK | InputEvent.CTRL_DOWN_MASK | InputEvent.CTRL_MASK,
342 false),
343 },
344 {},
345 {},
346 };
347
348
349
350
351
352
353
354 private Set[] defaultFocusTraversalKeys = new Set[4];
355
356
357
358
359
360
361
362
363 private static Container currentFocusCycleRoot;
364
365
366
367
368 private VetoableChangeSupport vetoableSupport;
369
370
371
372
373 private PropertyChangeSupport changeSupport;
374
375
376
377
378
379
380
381
382 private java.util.LinkedList keyEventDispatchers;
383
384
385
386
387
388
389
390
391 private java.util.LinkedList keyEventPostProcessors;
392
393
394
395
396 private static java.util.Map mostRecentFocusOwners = new WeakHashMap();
397
398
399
400
401 private static final String notPrivileged = "this KeyboardFocusManager is not installed in the current thread's context";
402
403
404
405
406
407 private static AWTPermission replaceKeyboardFocusManagerPermission;
408
409
410
411
412 transient SequencedEvent currentSequencedEvent = null;
413
414 final void setCurrentSequencedEvent(SequencedEvent current) {
415 synchronized (SequencedEvent.class) {
416 assert(current == null || currentSequencedEvent == null);
417 currentSequencedEvent = current;
418 }
419 }
420
421 final SequencedEvent getCurrentSequencedEvent() {
422 synchronized (SequencedEvent.class) {
423 return currentSequencedEvent;
424 }
425 }
426
427 static Set initFocusTraversalKeysSet(String value, Set targetSet) {
428 StringTokenizer tokens = new StringTokenizer(value, ",");
429 while (tokens.hasMoreTokens()) {
430 targetSet.add(AWTKeyStroke.getAWTKeyStroke(tokens.nextToken()));
431 }
432 return (targetSet.isEmpty())
433 ? Collections.EMPTY_SET
434 : Collections.unmodifiableSet(targetSet);
435 }
436
437
438
439
440 public KeyboardFocusManager() {
441 for (int i = 0; i < TRAVERSAL_KEY_LENGTH; i++) {
442 Set work_set = new HashSet();
443 for (int j = 0; j < defaultFocusTraversalKeyStrokes[i].length; j++) {
444 work_set.add(defaultFocusTraversalKeyStrokes[i][j]);
445 }
446 defaultFocusTraversalKeys[i] = (work_set.isEmpty())
447 ? Collections.EMPTY_SET
448 : Collections.unmodifiableSet(work_set);
449 }
450 initPeer();
451 }
452
453 private void initPeer() {
454 Toolkit tk = Toolkit.getDefaultToolkit();
455 KeyboardFocusManagerPeerProvider peerProvider = (KeyboardFocusManagerPeerProvider)tk;
456 peer = peerProvider.createKeyboardFocusManagerPeer(this);
457 }
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473 public Component getFocusOwner() {
474 synchronized (KeyboardFocusManager.class) {
475 if (focusOwner == null) {
476 return null;
477 }
478
479 return (focusOwner.appContext == AppContext.getAppContext())
480 ? focusOwner
481 : null;
482 }
483 }
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504 protected Component getGlobalFocusOwner() throws SecurityException {
505 synchronized (KeyboardFocusManager.class) {
506 if (this == getCurrentKeyboardFocusManager()) {
507 return focusOwner;
508 } else {
509 if (focusLog.isLoggable(PlatformLogger.FINER)) {
510 focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
511 }
512 throw new SecurityException(notPrivileged);
513 }
514 }
515 }
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541 protected void setGlobalFocusOwner(Component focusOwner) {
542 Component oldFocusOwner = null;
543 boolean shouldFire = false;
544
545 if (focusOwner == null || focusOwner.isFocusable()) {
546 synchronized (KeyboardFocusManager.class) {
547 oldFocusOwner = getFocusOwner();
548
549 try {
550 fireVetoableChange("focusOwner", oldFocusOwner,
551 focusOwner);
552 } catch (PropertyVetoException e) {
553
554 return;
555 }
556
557 KeyboardFocusManager.focusOwner = focusOwner;
558
559 if (focusOwner != null &&
560 (getCurrentFocusCycleRoot() == null ||
561 !focusOwner.isFocusCycleRoot(getCurrentFocusCycleRoot())))
562 {
563 Container rootAncestor =
564 focusOwner.getFocusCycleRootAncestor();
565 if (rootAncestor == null && (focusOwner instanceof Window))
566 {
567 rootAncestor = (Container)focusOwner;
568 }
569 if (rootAncestor != null) {
570 setGlobalCurrentFocusCycleRoot(rootAncestor);
571 }
572 }
573
574 shouldFire = true;
575 }
576 }
577
578 if (shouldFire) {
579 firePropertyChange("focusOwner", oldFocusOwner, focusOwner);
580 }
581 }
582
583
584
585
586
587
588
589
590
591
592
593
594
595 public void clearGlobalFocusOwner() {
596 if (!GraphicsEnvironment.isHeadless()) {
597
598
599 Toolkit.getDefaultToolkit();
600
601 _clearGlobalFocusOwner();
602 }
603 }
604 private void _clearGlobalFocusOwner() {
605 Window activeWindow = markClearGlobalFocusOwner();
606 peer.clearGlobalFocusOwner(activeWindow);
607 }
608
609 Component getNativeFocusOwner() {
610 return peer.getCurrentFocusOwner();
611 }
612
613 void setNativeFocusOwner(Component comp) {
614 if (focusLog.isLoggable(PlatformLogger.FINEST)) {
615 focusLog.finest("Calling peer {0} setCurrentFocusOwner for {1}",
616 String.valueOf(peer), String.valueOf(comp));
617 }
618 peer.setCurrentFocusOwner(comp);
619 }
620
621 Window getNativeFocusedWindow() {
622 return peer.getCurrentFocusedWindow();
623 }
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639 public Component getPermanentFocusOwner() {
640 synchronized (KeyboardFocusManager.class) {
641 if (permanentFocusOwner == null) {
642 return null;
643 }
644
645 return (permanentFocusOwner.appContext ==
646 AppContext.getAppContext())
647 ? permanentFocusOwner
648 : null;
649 }
650 }
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671 protected Component getGlobalPermanentFocusOwner()
672 throws SecurityException
673 {
674 synchronized (KeyboardFocusManager.class) {
675 if (this == getCurrentKeyboardFocusManager()) {
676 return permanentFocusOwner;
677 } else {
678 if (focusLog.isLoggable(PlatformLogger.FINER)) {
679 focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
680 }
681 throw new SecurityException(notPrivileged);
682 }
683 }
684 }
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711 protected void setGlobalPermanentFocusOwner(Component permanentFocusOwner)
712 {
713 Component oldPermanentFocusOwner = null;
714 boolean shouldFire = false;
715
716 if (permanentFocusOwner == null || permanentFocusOwner.isFocusable()) {
717 synchronized (KeyboardFocusManager.class) {
718 oldPermanentFocusOwner = getPermanentFocusOwner();
719
720 try {
721 fireVetoableChange("permanentFocusOwner",
722 oldPermanentFocusOwner,
723 permanentFocusOwner);
724 } catch (PropertyVetoException e) {
725
726 return;
727 }
728
729 KeyboardFocusManager.permanentFocusOwner = permanentFocusOwner;
730
731 KeyboardFocusManager.
732 setMostRecentFocusOwner(permanentFocusOwner);
733
734 shouldFire = true;
735 }
736 }
737
738 if (shouldFire) {
739 firePropertyChange("permanentFocusOwner", oldPermanentFocusOwner,
740 permanentFocusOwner);
741 }
742 }
743
744
745
746
747
748
749
750
751
752
753
754 public Window getFocusedWindow() {
755 synchronized (KeyboardFocusManager.class) {
756 if (focusedWindow == null) {
757 return null;
758 }
759
760 return (focusedWindow.appContext == AppContext.getAppContext())
761 ? focusedWindow
762 : null;
763 }
764 }
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781 protected Window getGlobalFocusedWindow() throws SecurityException {
782 synchronized (KeyboardFocusManager.class) {
783 if (this == getCurrentKeyboardFocusManager()) {
784 return focusedWindow;
785 } else {
786 if (focusLog.isLoggable(PlatformLogger.FINER)) {
787 focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
788 }
789 throw new SecurityException(notPrivileged);
790 }
791 }
792 }
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815 protected void setGlobalFocusedWindow(Window focusedWindow) {
816 Window oldFocusedWindow = null;
817 boolean shouldFire = false;
818
819 if (focusedWindow == null || focusedWindow.isFocusableWindow()) {
820 synchronized (KeyboardFocusManager.class) {
821 oldFocusedWindow = getFocusedWindow();
822
823 try {
824 fireVetoableChange("focusedWindow", oldFocusedWindow,
825 focusedWindow);
826 } catch (PropertyVetoException e) {
827
828 return;
829 }
830
831 KeyboardFocusManager.focusedWindow = focusedWindow;
832 shouldFire = true;
833 }
834 }
835
836 if (shouldFire) {
837 firePropertyChange("focusedWindow", oldFocusedWindow,
838 focusedWindow);
839 }
840 }
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855 public Window getActiveWindow() {
856 synchronized (KeyboardFocusManager.class) {
857 if (activeWindow == null) {
858 return null;
859 }
860
861 return (activeWindow.appContext == AppContext.getAppContext())
862 ? activeWindow
863 : null;
864 }
865 }
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885 protected Window getGlobalActiveWindow() throws SecurityException {
886 synchronized (KeyboardFocusManager.class) {
887 if (this == getCurrentKeyboardFocusManager()) {
888 return activeWindow;
889 } else {
890 if (focusLog.isLoggable(PlatformLogger.FINER)) {
891 focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
892 }
893 throw new SecurityException(notPrivileged);
894 }
895 }
896 }
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920 protected void setGlobalActiveWindow(Window activeWindow) {
921 Window oldActiveWindow;
922 synchronized (KeyboardFocusManager.class) {
923 oldActiveWindow = getActiveWindow();
924 if (focusLog.isLoggable(PlatformLogger.FINER)) {
925 focusLog.finer("Setting global active window to " + activeWindow + ", old active " + oldActiveWindow);
926 }
927
928 try {
929 fireVetoableChange("activeWindow", oldActiveWindow,
930 activeWindow);
931 } catch (PropertyVetoException e) {
932
933 return;
934 }
935
936 KeyboardFocusManager.activeWindow = activeWindow;
937 }
938
939 firePropertyChange("activeWindow", oldActiveWindow, activeWindow);
940 }
941
942
943
944
945
946
947
948
949
950
951
952 public synchronized FocusTraversalPolicy getDefaultFocusTraversalPolicy() {
953 return defaultPolicy;
954 }
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972 public void setDefaultFocusTraversalPolicy(FocusTraversalPolicy
973 defaultPolicy) {
974 if (defaultPolicy == null) {
975 throw new IllegalArgumentException("default focus traversal policy cannot be null");
976 }
977
978 FocusTraversalPolicy oldPolicy;
979
980 synchronized (this) {
981 oldPolicy = this.defaultPolicy;
982 this.defaultPolicy = defaultPolicy;
983 }
984
985 firePropertyChange("defaultFocusTraversalPolicy", oldPolicy,
986 defaultPolicy);
987 }
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076 public void
1077 setDefaultFocusTraversalKeys(int id,
1078 Set<? extends AWTKeyStroke> keystrokes)
1079 {
1080 if (id < 0 || id >= TRAVERSAL_KEY_LENGTH) {
1081 throw new IllegalArgumentException("invalid focus traversal key identifier");
1082 }
1083 if (keystrokes == null) {
1084 throw new IllegalArgumentException("cannot set null Set of default focus traversal keys");
1085 }
1086
1087 Set oldKeys;
1088
1089 synchronized (this) {
1090 for (Iterator iter = keystrokes.iterator(); iter.hasNext(); ) {
1091 Object obj = iter.next();
1092
1093 if (obj == null) {
1094 throw new IllegalArgumentException("cannot set null focus traversal key");
1095 }
1096
1097
1098
1099 if (!(obj instanceof AWTKeyStroke)) {
1100 throw new IllegalArgumentException("object is expected to be AWTKeyStroke");
1101 }
1102 AWTKeyStroke keystroke = (AWTKeyStroke)obj;
1103
1104 if (keystroke.getKeyChar() != KeyEvent.CHAR_UNDEFINED) {
1105 throw new IllegalArgumentException("focus traversal keys cannot map to KEY_TYPED events");
1106 }
1107
1108
1109
1110 for (int i = 0; i < TRAVERSAL_KEY_LENGTH; i++) {
1111 if (i == id) {
1112 continue;
1113 }
1114
1115 if (defaultFocusTraversalKeys[i].contains(keystroke)) {
1116 throw new IllegalArgumentException("focus traversal keys must be unique for a Component");
1117 }
1118 }
1119 }
1120
1121 oldKeys = defaultFocusTraversalKeys[id];
1122 defaultFocusTraversalKeys[id] =
1123 Collections.unmodifiableSet(new HashSet(keystrokes));
1124 }
1125
1126 firePropertyChange(defaultFocusTraversalKeyPropertyNames[id],
1127 oldKeys, keystrokes);
1128 }
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156 public Set<AWTKeyStroke> getDefaultFocusTraversalKeys(int id) {
1157 if (id < 0 || id >= TRAVERSAL_KEY_LENGTH) {
1158 throw new IllegalArgumentException("invalid focus traversal key identifier");
1159 }
1160
1161
1162 return defaultFocusTraversalKeys[id];
1163 }
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181 public Container getCurrentFocusCycleRoot() {
1182 synchronized (KeyboardFocusManager.class) {
1183 if (currentFocusCycleRoot == null) {
1184 return null;
1185 }
1186
1187 return (currentFocusCycleRoot.appContext ==
1188 AppContext.getAppContext())
1189 ? currentFocusCycleRoot
1190 : null;
1191 }
1192 }
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213 protected Container getGlobalCurrentFocusCycleRoot()
1214 throws SecurityException
1215 {
1216 synchronized (KeyboardFocusManager.class) {
1217 if (this == getCurrentKeyboardFocusManager()) {
1218 return currentFocusCycleRoot;
1219 } else {
1220 if (focusLog.isLoggable(PlatformLogger.FINER)) {
1221 focusLog.finer("This manager is " + this + ", current is " + getCurrentKeyboardFocusManager());
1222 }
1223 throw new SecurityException(notPrivileged);
1224 }
1225 }
1226 }
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244 public void setGlobalCurrentFocusCycleRoot(Container newFocusCycleRoot) {
1245 Container oldFocusCycleRoot;
1246
1247 synchronized (KeyboardFocusManager.class) {
1248 oldFocusCycleRoot = getCurrentFocusCycleRoot();
1249 currentFocusCycleRoot = newFocusCycleRoot;
1250 }
1251
1252 firePropertyChange("currentFocusCycleRoot", oldFocusCycleRoot,
1253 newFocusCycleRoot);
1254 }
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287 public void addPropertyChangeListener(PropertyChangeListener listener) {
1288 if (listener != null) {
1289 synchronized (this) {
1290 if (changeSupport == null) {
1291 changeSupport = new PropertyChangeSupport(this);
1292 }
1293 changeSupport.addPropertyChangeListener(listener);
1294 }
1295 }
1296 }
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310 public void removePropertyChangeListener(PropertyChangeListener listener) {
1311 if (listener != null) {
1312 synchronized (this) {
1313 if (changeSupport != null) {
1314 changeSupport.removePropertyChangeListener(listener);
1315 }
1316 }
1317 }
1318 }
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334 public synchronized PropertyChangeListener[] getPropertyChangeListeners() {
1335 if (changeSupport == null) {
1336 changeSupport = new PropertyChangeSupport(this);
1337 }
1338 return changeSupport.getPropertyChangeListeners();
1339 }
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373 public void addPropertyChangeListener(String propertyName,
1374 PropertyChangeListener listener) {
1375 if (listener != null) {
1376 synchronized (this) {
1377 if (changeSupport == null) {
1378 changeSupport = new PropertyChangeSupport(this);
1379 }
1380 changeSupport.addPropertyChangeListener(propertyName,
1381 listener);
1382 }
1383 }
1384 }
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399 public void removePropertyChangeListener(String propertyName,
1400 PropertyChangeListener listener) {
1401 if (listener != null) {
1402 synchronized (this) {
1403 if (changeSupport != null) {
1404 changeSupport.removePropertyChangeListener(propertyName,
1405 listener);
1406 }
1407 }
1408 }
1409 }
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423 public synchronized PropertyChangeListener[] getPropertyChangeListeners(String propertyName) {
1424 if (changeSupport == null) {
1425 changeSupport = new PropertyChangeSupport(this);
1426 }
1427 return changeSupport.getPropertyChangeListeners(propertyName);
1428 }
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439 protected void firePropertyChange(String propertyName, Object oldValue,
1440 Object newValue)
1441 {
1442 if (oldValue == newValue) {
1443 return;
1444 }
1445 PropertyChangeSupport changeSupport = this.changeSupport;
1446 if (changeSupport != null) {
1447 changeSupport.firePropertyChange(propertyName, oldValue, newValue);
1448 }
1449 }
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468 public void addVetoableChangeListener(VetoableChangeListener listener) {
1469 if (listener != null) {
1470 synchronized (this) {
1471 if (vetoableSupport == null) {
1472 vetoableSupport =
1473 new VetoableChangeSupport(this);
1474 }
1475 vetoableSupport.addVetoableChangeListener(listener);
1476 }
1477 }
1478 }
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492 public void removeVetoableChangeListener(VetoableChangeListener listener) {
1493 if (listener != null) {
1494 synchronized (this) {
1495 if (vetoableSupport != null) {
1496 vetoableSupport.removeVetoableChangeListener(listener);
1497 }
1498 }
1499 }
1500 }
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516 public synchronized VetoableChangeListener[] getVetoableChangeListeners() {
1517 if (vetoableSupport == null) {
1518 vetoableSupport = new VetoableChangeSupport(this);
1519 }
1520 return vetoableSupport.getVetoableChangeListeners();
1521 }
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541 public void addVetoableChangeListener(String propertyName,
1542 VetoableChangeListener listener) {
1543 if (listener != null) {
1544 synchronized (this) {
1545 if (vetoableSupport == null) {
1546 vetoableSupport =
1547 new VetoableChangeSupport(this);
1548 }
1549 vetoableSupport.addVetoableChangeListener(propertyName,
1550 listener);
1551 }
1552 }
1553 }
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568 public void removeVetoableChangeListener(String propertyName,
1569 VetoableChangeListener listener) {
1570 if (listener != null) {
1571 synchronized (this) {
1572 if (vetoableSupport != null) {
1573 vetoableSupport.removeVetoableChangeListener(propertyName,
1574 listener);
1575 }
1576 }
1577 }
1578 }
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593 public synchronized VetoableChangeListener[] getVetoableChangeListeners(String propertyName) {
1594 if (vetoableSupport == null) {
1595 vetoableSupport = new VetoableChangeSupport(this);
1596 }
1597 return vetoableSupport.getVetoableChangeListeners(propertyName);
1598 }
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616 protected void fireVetoableChange(String propertyName, Object oldValue,
1617 Object newValue)
1618 throws PropertyVetoException
1619 {
1620 if (oldValue == newValue) {
1621 return;
1622 }
1623 VetoableChangeSupport vetoableSupport =
1624 this.vetoableSupport;
1625 if (vetoableSupport != null) {
1626 vetoableSupport.fireVetoableChange(propertyName, oldValue,
1627 newValue);
1628 }
1629 }
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653 public void addKeyEventDispatcher(KeyEventDispatcher dispatcher) {
1654 if (dispatcher != null) {
1655 synchronized (this) {
1656 if (keyEventDispatchers == null) {
1657 keyEventDispatchers = new java.util.LinkedList();
1658 }
1659 keyEventDispatchers.add(dispatcher);
1660 }
1661 }
1662 }
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684 public void removeKeyEventDispatcher(KeyEventDispatcher dispatcher) {
1685 if (dispatcher != null) {
1686 synchronized (this) {
1687 if (keyEventDispatchers != null) {
1688 keyEventDispatchers.remove(dispatcher);
1689 }
1690 }
1691 }
1692 }
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707 protected synchronized java.util.List<KeyEventDispatcher>
1708 getKeyEventDispatchers()
1709 {
1710 return (keyEventDispatchers != null)
1711 ? (java.util.List)keyEventDispatchers.clone()
1712 : null;
1713 }
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741 public void addKeyEventPostProcessor(KeyEventPostProcessor processor) {
1742 if (processor != null) {
1743 synchronized (this) {
1744 if (keyEventPostProcessors == null) {
1745 keyEventPostProcessors = new java.util.LinkedList();
1746 }
1747 keyEventPostProcessors.add(processor);
1748 }
1749 }
1750 }
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774 public void removeKeyEventPostProcessor(KeyEventPostProcessor processor) {
1775 if (processor != null) {
1776 synchronized (this) {
1777 if (keyEventPostProcessors != null) {
1778 keyEventPostProcessors.remove(processor);
1779 }
1780 }
1781 }
1782 }
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798 protected java.util.List<KeyEventPostProcessor>
1799 getKeyEventPostProcessors()
1800 {
1801 return (keyEventPostProcessors != null)
1802 ? (java.util.List)keyEventPostProcessors.clone()
1803 : null;
1804 }
1805
1806
1807
1808 static void setMostRecentFocusOwner(Component component) {
1809 Component window = component;
1810 while (window != null && !(window instanceof Window)) {
1811 window = window.parent;
1812 }
1813 if (window != null) {
1814 setMostRecentFocusOwner((Window)window, component);
1815 }
1816 }
1817 static synchronized void setMostRecentFocusOwner(Window window,
1818 Component component) {
1819
1820
1821
1822
1823 WeakReference weakValue = null;
1824 if (component != null) {
1825 weakValue = new WeakReference(component);
1826 }
1827 mostRecentFocusOwners.put(window, weakValue);
1828 }
1829 static void clearMostRecentFocusOwner(Component comp) {
1830 Container window;
1831
1832 if (comp == null) {
1833 return;
1834 }
1835
1836 synchronized (comp.getTreeLock()) {
1837 window = comp.getParent();
1838 while (window != null && !(window instanceof Window)) {
1839 window = window.getParent();
1840 }
1841 }
1842
1843 synchronized (KeyboardFocusManager.class) {
1844 if ((window != null)
1845 && (getMostRecentFocusOwner((Window)window) == comp))
1846 {
1847 setMostRecentFocusOwner((Window)window, null);
1848 }
1849
1850 if (window != null) {
1851 Window realWindow = (Window)window;
1852 if (realWindow.getTemporaryLostComponent() == comp) {
1853 realWindow.setTemporaryLostComponent(null);
1854 }
1855 }
1856 }
1857 }
1858
1859
1860
1861
1862
1863 static synchronized Component getMostRecentFocusOwner(Window window) {
1864 WeakReference weakValue =
1865 (WeakReference)mostRecentFocusOwners.get(window);
1866 return weakValue == null ? null : (Component)weakValue.get();
1867 }
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888 public abstract boolean dispatchEvent(AWTEvent e);
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906 public final void redispatchEvent(Component target, AWTEvent e) {
1907 e.focusManagerIsDispatching = true;
1908 target.dispatchEvent(e);
1909 e.focusManagerIsDispatching = false;
1910 }
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927 public abstract boolean dispatchKeyEvent(KeyEvent e);
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941 public abstract boolean postProcessKeyEvent(KeyEvent e);
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956 public abstract void processKeyEvent(Component focusedComponent,
1957 KeyEvent e);
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979 protected abstract void enqueueKeyEvents(long after,
1980 Component untilFocused);
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998 protected abstract void dequeueKeyEvents(long after,
1999 Component untilFocused);
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012 protected abstract void discardKeyEvents(Component comp);
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022 public abstract void focusNextComponent(Component aComponent);
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032 public abstract void focusPreviousComponent(Component aComponent);
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045 public abstract void upFocusCycle(Component aComponent);
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057 public abstract void downFocusCycle(Container aContainer);
2058
2059
2060
2061
2062 public final void focusNextComponent() {
2063 Component focusOwner = getFocusOwner();
2064 if (focusOwner != null) {
2065 focusNextComponent(focusOwner);
2066 }
2067 }
2068
2069
2070
2071
2072 public final void focusPreviousComponent() {
2073 Component focusOwner = getFocusOwner();
2074 if (focusOwner != null) {
2075 focusPreviousComponent(focusOwner);
2076 }
2077 }
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088 public final void upFocusCycle() {
2089 Component focusOwner = getFocusOwner();
2090 if (focusOwner != null) {
2091 upFocusCycle(focusOwner);
2092 }
2093 }
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104 public final void downFocusCycle() {
2105 Component focusOwner = getFocusOwner();
2106 if (focusOwner instanceof Container) {
2107 downFocusCycle((Container)focusOwner);
2108 }
2109 }
2110
2111
2112
2113
2114 void dumpRequests() {
2115 System.err.println(">>> Requests dump, time: " + System.currentTimeMillis());
2116 synchronized (heavyweightRequests) {
2117 for (HeavyweightFocusRequest req : heavyweightRequests) {
2118 System.err.println(">>> Req: " + req);
2119 }
2120 }
2121 System.err.println("");
2122 }
2123
2124 private static final class LightweightFocusRequest {
2125 final Component component;
2126 final boolean temporary;
2127 final CausedFocusEvent.Cause cause;
2128
2129 LightweightFocusRequest(Component component, boolean temporary, CausedFocusEvent.Cause cause) {
2130 this.component = component;
2131 this.temporary = temporary;
2132 this.cause = cause;
2133 }
2134 public String toString() {
2135 return "LightweightFocusRequest[component=" + component +
2136 ",temporary=" + temporary + ", cause=" + cause + "]";
2137 }
2138 }
2139
2140 private static final class HeavyweightFocusRequest {
2141 final Component heavyweight;
2142 final LinkedList<LightweightFocusRequest> lightweightRequests;
2143
2144 static final HeavyweightFocusRequest CLEAR_GLOBAL_FOCUS_OWNER =
2145 new HeavyweightFocusRequest();
2146
2147 private HeavyweightFocusRequest() {
2148 heavyweight = null;
2149 lightweightRequests = null;
2150 }
2151
2152 HeavyweightFocusRequest(Component heavyweight, Component descendant,
2153 boolean temporary, CausedFocusEvent.Cause cause) {
2154 if (log.isLoggable(PlatformLogger.FINE)) {
2155 if (heavyweight == null) {
2156 log.fine("Assertion (heavyweight != null) failed");
2157 }
2158 }
2159
2160 this.heavyweight = heavyweight;
2161 this.lightweightRequests = new LinkedList<LightweightFocusRequest>();
2162 addLightweightRequest(descendant, temporary, cause);
2163 }
2164 boolean addLightweightRequest(Component descendant,
2165 boolean temporary, CausedFocusEvent.Cause cause) {
2166 if (log.isLoggable(PlatformLogger.FINE)) {
2167 if (this == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) {
2168 log.fine("Assertion (this != HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER) failed");
2169 }
2170 if (descendant == null) {
2171 log.fine("Assertion (descendant != null) failed");
2172 }
2173 }
2174
2175 Component lastDescendant = ((lightweightRequests.size() > 0)
2176 ? lightweightRequests.getLast().component
2177 : null);
2178
2179 if (descendant != lastDescendant) {
2180
2181 lightweightRequests.add
2182 (new LightweightFocusRequest(descendant, temporary, cause));
2183 return true;
2184 } else {
2185 return false;
2186 }
2187 }
2188
2189 LightweightFocusRequest getFirstLightweightRequest() {
2190 if (this == CLEAR_GLOBAL_FOCUS_OWNER) {
2191 return null;
2192 }
2193 return lightweightRequests.getFirst();
2194 }
2195 public String toString() {
2196 boolean first = true;
2197 String str = "HeavyweightFocusRequest[heavweight=" + heavyweight +
2198 ",lightweightRequests=";
2199 if (lightweightRequests == null) {
2200 str += null;
2201 } else {
2202 str += "[";
2203
2204 for (LightweightFocusRequest lwRequest : lightweightRequests) {
2205 if (first) {
2206 first = false;
2207 } else {
2208 str += ",";
2209 }
2210 str += lwRequest;
2211 }
2212 str += "]";
2213 }
2214 str += "]";
2215 return str;
2216 }
2217 }
2218
2219
2220
2221
2222
2223
2224 private static LinkedList<HeavyweightFocusRequest> heavyweightRequests =
2225 new LinkedList<HeavyweightFocusRequest>();
2226 private static LinkedList<LightweightFocusRequest> currentLightweightRequests;
2227 private static boolean clearingCurrentLightweightRequests;
2228 private static boolean allowSyncFocusRequests = true;
2229 private static Component newFocusOwner = null;
2230 private static volatile boolean disableRestoreFocus;
2231
2232 static final int SNFH_FAILURE = 0;
2233 static final int SNFH_SUCCESS_HANDLED = 1;
2234 static final int SNFH_SUCCESS_PROCEED = 2;
2235
2236 static boolean processSynchronousLightweightTransfer(Component heavyweight, Component descendant,
2237 boolean temporary, boolean focusedWindowChangeAllowed,
2238 long time)
2239 {
2240 Window parentWindow = SunToolkit.getContainingWindow(heavyweight);
2241 if (parentWindow == null || !parentWindow.syncLWRequests) {
2242 return false;
2243 }
2244 if (descendant == null) {
2245
2246
2247
2248 descendant = heavyweight;
2249 }
2250
2251 KeyboardFocusManager manager = getCurrentKeyboardFocusManager(SunToolkit.targetToAppContext(descendant));
2252
2253 FocusEvent currentFocusOwnerEvent = null;
2254 FocusEvent newFocusOwnerEvent = null;
2255 Component currentFocusOwner = manager.getGlobalFocusOwner();
2256
2257 synchronized (heavyweightRequests) {
2258 HeavyweightFocusRequest hwFocusRequest = getLastHWRequest();
2259 if (hwFocusRequest == null &&
2260 heavyweight == manager.getNativeFocusOwner() &&
2261 allowSyncFocusRequests)
2262 {
2263
2264 if (descendant == currentFocusOwner) {
2265
2266 return true;
2267 }
2268
2269
2270
2271
2272
2273 manager.enqueueKeyEvents(time, descendant);
2274
2275 hwFocusRequest =
2276 new HeavyweightFocusRequest(heavyweight, descendant,
2277 temporary, CausedFocusEvent.Cause.UNKNOWN);
2278 heavyweightRequests.add(hwFocusRequest);
2279
2280 if (currentFocusOwner != null) {
2281 currentFocusOwnerEvent =
2282 new FocusEvent(currentFocusOwner,
2283 FocusEvent.FOCUS_LOST,
2284 temporary, descendant);
2285 }
2286 newFocusOwnerEvent =
2287 new FocusEvent(descendant, FocusEvent.FOCUS_GAINED,
2288 temporary, currentFocusOwner);
2289 }
2290 }
2291 boolean result = false;
2292 final boolean clearing = clearingCurrentLightweightRequests;
2293
2294 Throwable caughtEx = null;
2295 try {
2296 clearingCurrentLightweightRequests = false;
2297 synchronized(Component.LOCK) {
2298
2299 if (currentFocusOwnerEvent != null && currentFocusOwner != null) {
2300 ((AWTEvent) currentFocusOwnerEvent).isPosted = true;
2301 caughtEx = dispatchAndCatchException(caughtEx, currentFocusOwner, currentFocusOwnerEvent);
2302 result = true;
2303 }
2304
2305 if (newFocusOwnerEvent != null && descendant != null) {
2306 ((AWTEvent) newFocusOwnerEvent).isPosted = true;
2307 caughtEx = dispatchAndCatchException(caughtEx, descendant, newFocusOwnerEvent);
2308 result = true;
2309 }
2310 }
2311 } finally {
2312 clearingCurrentLightweightRequests = clearing;
2313 }
2314 if (caughtEx instanceof RuntimeException) {
2315 throw (RuntimeException)caughtEx;
2316 } else if (caughtEx instanceof Error) {
2317 throw (Error)caughtEx;
2318 }
2319 return result;
2320 }
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340 static int shouldNativelyFocusHeavyweight
2341 (Component heavyweight, Component descendant, boolean temporary,
2342 boolean focusedWindowChangeAllowed, long time, CausedFocusEvent.Cause cause)
2343 {
2344 if (log.isLoggable(PlatformLogger.FINE)) {
2345 if (heavyweight == null) {
2346 log.fine("Assertion (heavyweight != null) failed");
2347 }
2348 if (time == 0) {
2349 log.fine("Assertion (time != 0) failed");
2350 }
2351 }
2352
2353 if (descendant == null) {
2354
2355
2356
2357 descendant = heavyweight;
2358 }
2359
2360 KeyboardFocusManager manager =
2361 getCurrentKeyboardFocusManager(SunToolkit.targetToAppContext(descendant));
2362 KeyboardFocusManager thisManager = getCurrentKeyboardFocusManager();
2363 Component currentFocusOwner = thisManager.getGlobalFocusOwner();
2364 Component nativeFocusOwner = thisManager.getNativeFocusOwner();
2365 Window nativeFocusedWindow = thisManager.getNativeFocusedWindow();
2366 if (focusLog.isLoggable(PlatformLogger.FINER)) {
2367 focusLog.finer("SNFH for {0} in {1}",
2368 String.valueOf(descendant), String.valueOf(heavyweight));
2369 }
2370 if (focusLog.isLoggable(PlatformLogger.FINEST)) {
2371 focusLog.finest("0. Current focus owner {0}",
2372 String.valueOf(currentFocusOwner));
2373 focusLog.finest("0. Native focus owner {0}",
2374 String.valueOf(nativeFocusOwner));
2375 focusLog.finest("0. Native focused window {0}",
2376 String.valueOf(nativeFocusedWindow));
2377 }
2378 synchronized (heavyweightRequests) {
2379 HeavyweightFocusRequest hwFocusRequest = getLastHWRequest();
2380 if (focusLog.isLoggable(PlatformLogger.FINEST)) {
2381 focusLog.finest("Request {0}", String.valueOf(hwFocusRequest));
2382 }
2383 if (hwFocusRequest == null &&
2384 heavyweight == nativeFocusOwner)
2385 {
2386 if (descendant == currentFocusOwner) {
2387
2388 if (focusLog.isLoggable(PlatformLogger.FINEST))
2389 focusLog.finest("1. SNFH_FAILURE for {0}",
2390 String.valueOf(descendant));
2391 return SNFH_FAILURE;
2392 }
2393
2394
2395
2396
2397
2398 manager.enqueueKeyEvents(time, descendant);
2399
2400 hwFocusRequest =
2401 new HeavyweightFocusRequest(heavyweight, descendant,
2402 temporary, cause);
2403 heavyweightRequests.add(hwFocusRequest);
2404
2405 if (currentFocusOwner != null) {
2406 FocusEvent currentFocusOwnerEvent =
2407 new CausedFocusEvent(currentFocusOwner,
2408 FocusEvent.FOCUS_LOST,
2409 temporary, descendant, cause);
2410
2411
2412 SunToolkit.postEvent(currentFocusOwner.appContext,
2413 currentFocusOwnerEvent);
2414 }
2415 FocusEvent newFocusOwnerEvent =
2416 new CausedFocusEvent(descendant, FocusEvent.FOCUS_GAINED,
2417 temporary, currentFocusOwner, cause);
2418
2419
2420 SunToolkit.postEvent(descendant.appContext, newFocusOwnerEvent);
2421
2422 if (focusLog.isLoggable(PlatformLogger.FINEST))
2423 focusLog.finest("2. SNFH_HANDLED for {0}", String.valueOf(descendant));
2424 return SNFH_SUCCESS_HANDLED;
2425 } else if (hwFocusRequest != null &&
2426 hwFocusRequest.heavyweight == heavyweight) {
2427
2428
2429
2430
2431 if (hwFocusRequest.addLightweightRequest(descendant,
2432 temporary, cause)) {
2433 manager.enqueueKeyEvents(time, descendant);
2434 }
2435
2436 if (focusLog.isLoggable(PlatformLogger.FINEST))
2437 focusLog.finest("3. SNFH_HANDLED for lightweight" +
2438 descendant + " in " + heavyweight);
2439 return SNFH_SUCCESS_HANDLED;
2440 } else {
2441 if (!focusedWindowChangeAllowed) {
2442
2443
2444
2445
2446
2447 if (hwFocusRequest ==
2448 HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER)
2449 {
2450 int size = heavyweightRequests.size();
2451 hwFocusRequest = (HeavyweightFocusRequest)((size >= 2)
2452 ? heavyweightRequests.get(size - 2)
2453 : null);
2454 }
2455 if (focusedWindowChanged(heavyweight,
2456 (hwFocusRequest != null)
2457 ? hwFocusRequest.heavyweight
2458 : nativeFocusedWindow)) {
2459 if (focusLog.isLoggable(PlatformLogger.FINEST))
2460 focusLog.finest("4. SNFH_FAILURE for " + descendant);
2461 return SNFH_FAILURE;
2462 }
2463 }
2464
2465 manager.enqueueKeyEvents(time, descendant);
2466 heavyweightRequests.add
2467 (new HeavyweightFocusRequest(heavyweight, descendant,
2468 temporary, cause));
2469 if (focusLog.isLoggable(PlatformLogger.FINEST))
2470 focusLog.finest("5. SNFH_PROCEED for " + descendant);
2471 return SNFH_SUCCESS_PROCEED;
2472 }
2473 }
2474 }
2475
2476
2477
2478
2479
2480
2481
2482
2483 static Window markClearGlobalFocusOwner() {
2484
2485
2486 final Component nativeFocusedWindow =
2487 getCurrentKeyboardFocusManager().getNativeFocusedWindow();
2488
2489 synchronized (heavyweightRequests) {
2490 HeavyweightFocusRequest hwFocusRequest = getLastHWRequest();
2491 if (hwFocusRequest ==
2492 HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER)
2493 {
2494
2495 return null;
2496 }
2497
2498 heavyweightRequests.add
2499 (HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER);
2500
2501 Component activeWindow = ((hwFocusRequest != null)
2502 ? SunToolkit.getContainingWindow(hwFocusRequest.heavyweight)
2503 : nativeFocusedWindow);
2504 while (activeWindow != null &&
2505 !((activeWindow instanceof Frame) ||
2506 (activeWindow instanceof Dialog)))
2507 {
2508 activeWindow = activeWindow.getParent_NoClientCode();
2509 }
2510
2511 return (Window) activeWindow;
2512 }
2513 }
2514 Component getCurrentWaitingRequest(Component parent) {
2515 synchronized (heavyweightRequests) {
2516 HeavyweightFocusRequest hwFocusRequest = getFirstHWRequest();
2517 if (hwFocusRequest != null) {
2518 if (hwFocusRequest.heavyweight == parent) {
2519 LightweightFocusRequest lwFocusRequest =
2520 hwFocusRequest.lightweightRequests.getFirst();
2521 if (lwFocusRequest != null) {
2522 return lwFocusRequest.component;
2523 }
2524 }
2525 }
2526 }
2527 return null;
2528 }
2529
2530 static boolean isAutoFocusTransferEnabled() {
2531 synchronized (heavyweightRequests) {
2532 return (heavyweightRequests.size() == 0)
2533 && !disableRestoreFocus
2534 && (null == currentLightweightRequests);
2535 }
2536 }
2537
2538 static boolean isAutoFocusTransferEnabledFor(Component comp) {
2539 return isAutoFocusTransferEnabled() && comp.isAutoFocusTransferOnDisposal();
2540 }
2541
2542
2543
2544
2545
2546
2547
2548 static private Throwable dispatchAndCatchException(Throwable ex, Component comp, FocusEvent event) {
2549 Throwable retEx = null;
2550 try {
2551 comp.dispatchEvent(event);
2552 } catch (RuntimeException re) {
2553 retEx = re;
2554 } catch (Error er) {
2555 retEx = er;
2556 }
2557 if (retEx != null) {
2558 if (ex != null) {
2559 handleException(ex);
2560 }
2561 return retEx;
2562 }
2563 return ex;
2564 }
2565
2566 static private void handleException(Throwable ex) {
2567 ex.printStackTrace();
2568 }
2569
2570 static void processCurrentLightweightRequests() {
2571 KeyboardFocusManager manager = getCurrentKeyboardFocusManager();
2572 LinkedList<LightweightFocusRequest> localLightweightRequests = null;
2573
2574 Component globalFocusOwner = manager.getGlobalFocusOwner();
2575 if ((globalFocusOwner != null) &&
2576 (globalFocusOwner.appContext != AppContext.getAppContext()))
2577 {
2578
2579
2580
2581 return;
2582 }
2583
2584 synchronized(heavyweightRequests) {
2585 if (currentLightweightRequests != null) {
2586 clearingCurrentLightweightRequests = true;
2587 disableRestoreFocus = true;
2588 localLightweightRequests = currentLightweightRequests;
2589 allowSyncFocusRequests = (localLightweightRequests.size() < 2);
2590 currentLightweightRequests = null;
2591 } else {
2592
2593 return;
2594 }
2595 }
2596
2597 Throwable caughtEx = null;
2598 try {
2599 if (localLightweightRequests != null) {
2600 Component lastFocusOwner = null;
2601 Component currentFocusOwner = null;
2602
2603 for (Iterator iter = localLightweightRequests.iterator(); iter.hasNext(); ) {
2604
2605 currentFocusOwner = manager.getGlobalFocusOwner();
2606 LightweightFocusRequest lwFocusRequest =
2607 (LightweightFocusRequest)iter.next();
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618 if (!iter.hasNext()) {
2619 disableRestoreFocus = false;
2620 }
2621
2622 FocusEvent currentFocusOwnerEvent = null;
2623
2624
2625
2626
2627
2628 if (currentFocusOwner != null) {
2629 currentFocusOwnerEvent = new CausedFocusEvent(currentFocusOwner,
2630 FocusEvent.FOCUS_LOST,
2631 lwFocusRequest.temporary,
2632 lwFocusRequest.component, lwFocusRequest.cause);
2633 }
2634 FocusEvent newFocusOwnerEvent =
2635 new CausedFocusEvent(lwFocusRequest.component,
2636 FocusEvent.FOCUS_GAINED,
2637 lwFocusRequest.temporary,
2638 currentFocusOwner == null ? lastFocusOwner : currentFocusOwner,
2639 lwFocusRequest.cause);
2640
2641 if (currentFocusOwner != null) {
2642 ((AWTEvent) currentFocusOwnerEvent).isPosted = true;
2643 caughtEx = dispatchAndCatchException(caughtEx, currentFocusOwner, currentFocusOwnerEvent);
2644 }
2645
2646 ((AWTEvent) newFocusOwnerEvent).isPosted = true;
2647 caughtEx = dispatchAndCatchException(caughtEx, lwFocusRequest.component, newFocusOwnerEvent);
2648
2649 if (manager.getGlobalFocusOwner() == lwFocusRequest.component) {
2650 lastFocusOwner = lwFocusRequest.component;
2651 }
2652 }
2653 }
2654 } finally {
2655 clearingCurrentLightweightRequests = false;
2656 disableRestoreFocus = false;
2657 localLightweightRequests = null;
2658 allowSyncFocusRequests = true;
2659 }
2660 if (caughtEx instanceof RuntimeException) {
2661 throw (RuntimeException)caughtEx;
2662 } else if (caughtEx instanceof Error) {
2663 throw (Error)caughtEx;
2664 }
2665 }
2666
2667 static FocusEvent retargetUnexpectedFocusEvent(FocusEvent fe) {
2668 synchronized (heavyweightRequests) {
2669
2670
2671
2672
2673 if (removeFirstRequest()) {
2674 return (FocusEvent)retargetFocusEvent(fe);
2675 }
2676
2677 Component source = fe.getComponent();
2678 Component opposite = fe.getOppositeComponent();
2679 boolean temporary = false;
2680 if (fe.getID() == FocusEvent.FOCUS_LOST &&
2681 (opposite == null || isTemporary(opposite, source)))
2682 {
2683 temporary = true;
2684 }
2685 return new CausedFocusEvent(source, fe.getID(), temporary, opposite,
2686 CausedFocusEvent.Cause.NATIVE_SYSTEM);
2687 }
2688 }
2689
2690 static FocusEvent retargetFocusGained(FocusEvent fe) {
2691 assert (fe.getID() == FocusEvent.FOCUS_GAINED);
2692
2693 Component currentFocusOwner = getCurrentKeyboardFocusManager().
2694 getGlobalFocusOwner();
2695 Component source = fe.getComponent();
2696 Component opposite = fe.getOppositeComponent();
2697 Component nativeSource = getHeavyweight(source);
2698
2699 synchronized (heavyweightRequests) {
2700 HeavyweightFocusRequest hwFocusRequest = getFirstHWRequest();
2701
2702 if (hwFocusRequest == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER)
2703 {
2704 return retargetUnexpectedFocusEvent(fe);
2705 }
2706
2707 if (source != null && nativeSource == null && hwFocusRequest != null) {
2708
2709
2710
2711 if (source == hwFocusRequest.getFirstLightweightRequest().component)
2712 {
2713 source = hwFocusRequest.heavyweight;
2714 nativeSource = source;
2715 }
2716 }
2717 if (hwFocusRequest != null &&
2718 nativeSource == hwFocusRequest.heavyweight)
2719 {
2720
2721
2722
2723 heavyweightRequests.removeFirst();
2724
2725 LightweightFocusRequest lwFocusRequest =
2726 hwFocusRequest.lightweightRequests.removeFirst();
2727
2728 Component newSource = lwFocusRequest.component;
2729 if (currentFocusOwner != null) {
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741 newFocusOwner = newSource;
2742 }
2743
2744 boolean temporary = (opposite == null ||
2745 isTemporary(newSource, opposite))
2746 ? false
2747 : lwFocusRequest.temporary;
2748
2749 if (hwFocusRequest.lightweightRequests.size() > 0) {
2750 currentLightweightRequests =
2751 hwFocusRequest.lightweightRequests;
2752 EventQueue.invokeLater(new Runnable() {
2753 public void run() {
2754 processCurrentLightweightRequests();
2755 }
2756 });
2757 }
2758
2759
2760
2761 return new CausedFocusEvent(newSource,
2762 FocusEvent.FOCUS_GAINED, temporary,
2763 opposite, lwFocusRequest.cause);
2764 }
2765
2766 if (currentFocusOwner != null
2767 && currentFocusOwner.getContainingWindow() == source
2768 && (hwFocusRequest == null || source != hwFocusRequest.heavyweight))
2769 {
2770
2771
2772
2773
2774 return new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_GAINED, false,
2775 null, CausedFocusEvent.Cause.ACTIVATION);
2776 }
2777
2778 return retargetUnexpectedFocusEvent(fe);
2779 }
2780 }
2781
2782 static FocusEvent retargetFocusLost(FocusEvent fe) {
2783 assert (fe.getID() == FocusEvent.FOCUS_LOST);
2784
2785 Component currentFocusOwner = getCurrentKeyboardFocusManager().
2786 getGlobalFocusOwner();
2787 Component opposite = fe.getOppositeComponent();
2788 Component nativeOpposite = getHeavyweight(opposite);
2789
2790 synchronized (heavyweightRequests) {
2791 HeavyweightFocusRequest hwFocusRequest = getFirstHWRequest();
2792
2793 if (hwFocusRequest == HeavyweightFocusRequest.CLEAR_GLOBAL_FOCUS_OWNER)
2794 {
2795 if (currentFocusOwner != null) {
2796
2797 heavyweightRequests.removeFirst();
2798 return new CausedFocusEvent(currentFocusOwner,
2799 FocusEvent.FOCUS_LOST, false, null,
2800 CausedFocusEvent.Cause.CLEAR_GLOBAL_FOCUS_OWNER);
2801 }
2802
2803
2804
2805 } else if (opposite == null)
2806 {
2807
2808 if (currentFocusOwner != null) {
2809 return new CausedFocusEvent(currentFocusOwner,
2810 FocusEvent.FOCUS_LOST,
2811 true, null, CausedFocusEvent.Cause.ACTIVATION);
2812 } else {
2813 return fe;
2814 }
2815 } else if (hwFocusRequest != null &&
2816 (nativeOpposite == hwFocusRequest.heavyweight ||
2817 nativeOpposite == null &&
2818 opposite == hwFocusRequest.getFirstLightweightRequest().component))
2819 {
2820 if (currentFocusOwner == null) {
2821 return fe;
2822 }
2823
2824
2825
2826
2827
2828
2829
2830 LightweightFocusRequest lwFocusRequest =
2831 hwFocusRequest.lightweightRequests.getFirst();
2832
2833 boolean temporary = isTemporary(opposite, currentFocusOwner)
2834 ? true
2835 : lwFocusRequest.temporary;
2836
2837 return new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST,
2838 temporary, lwFocusRequest.component, lwFocusRequest.cause);
2839 } else if (focusedWindowChanged(opposite, currentFocusOwner)) {
2840
2841
2842 if (!fe.isTemporary() && currentFocusOwner != null) {
2843
2844 fe = new CausedFocusEvent(currentFocusOwner, FocusEvent.FOCUS_LOST,
2845 true, opposite, CausedFocusEvent.Cause.ACTIVATION);
2846 }
2847 return fe;
2848 }
2849
2850 return retargetUnexpectedFocusEvent(fe);
2851 }
2852 }
2853
2854 static AWTEvent retargetFocusEvent(AWTEvent event) {
2855 if (clearingCurrentLightweightRequests) {
2856 return event;
2857 }
2858
2859 KeyboardFocusManager manager = getCurrentKeyboardFocusManager();
2860 if (focusLog.isLoggable(PlatformLogger.FINER)) {
2861 if (event instanceof FocusEvent || event instanceof WindowEvent) {
2862 focusLog.finer(">>> {0}", String.valueOf(event));
2863 }
2864 if (focusLog.isLoggable(PlatformLogger.FINER) && event instanceof KeyEvent) {
2865 focusLog.finer(" focus owner is {0}",
2866 String.valueOf(manager.getGlobalFocusOwner()));
2867 focusLog.finer(">>> {0}", String.valueOf(event));
2868 }
2869 }
2870
2871 synchronized(heavyweightRequests) {
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882 if (newFocusOwner != null &&
2883 event.getID() == FocusEvent.FOCUS_LOST)
2884 {
2885 FocusEvent fe = (FocusEvent)event;
2886
2887 if (manager.getGlobalFocusOwner() == fe.getComponent() &&
2888 fe.getOppositeComponent() == newFocusOwner)
2889 {
2890 newFocusOwner = null;
2891 return event;
2892 }
2893 }
2894 }
2895
2896 processCurrentLightweightRequests();
2897
2898 switch (event.getID()) {
2899 case FocusEvent.FOCUS_GAINED: {
2900 event = retargetFocusGained((FocusEvent)event);
2901 break;
2902 }
2903 case FocusEvent.FOCUS_LOST: {
2904 event = retargetFocusLost((FocusEvent)event);
2905 break;
2906 }
2907 default:
2908
2909 }
2910 return event;
2911 }
2912
2913
2914
2915
2916
2917
2918
2919 void clearMarkers() {
2920 }
2921
2922 static boolean removeFirstRequest() {
2923 KeyboardFocusManager manager =
2924 KeyboardFocusManager.getCurrentKeyboardFocusManager();
2925
2926 synchronized(heavyweightRequests) {
2927 HeavyweightFocusRequest hwFocusRequest = getFirstHWRequest();
2928
2929 if (hwFocusRequest != null) {
2930 heavyweightRequests.removeFirst();
2931 if (hwFocusRequest.lightweightRequests != null) {
2932 for (Iterator lwIter = hwFocusRequest.lightweightRequests.
2933 iterator();
2934 lwIter.hasNext(); )
2935 {
2936 manager.dequeueKeyEvents
2937 (-1, ((LightweightFocusRequest)lwIter.next()).
2938 component);
2939 }
2940 }
2941 }
2942
2943
2944 if (heavyweightRequests.size() == 0) {
2945 manager.clearMarkers();
2946 }
2947 return (heavyweightRequests.size() > 0);
2948 }
2949 }
2950 static void removeLastFocusRequest(Component heavyweight) {
2951 if (log.isLoggable(PlatformLogger.FINE)) {
2952 if (heavyweight == null) {
2953 log.fine("Assertion (heavyweight != null) failed");
2954 }
2955 }
2956
2957 KeyboardFocusManager manager =
2958 KeyboardFocusManager.getCurrentKeyboardFocusManager();
2959 synchronized(heavyweightRequests) {
2960 HeavyweightFocusRequest hwFocusRequest = getLastHWRequest();
2961 if (hwFocusRequest != null &&
2962 hwFocusRequest.heavyweight == heavyweight) {
2963 heavyweightRequests.removeLast();
2964 }
2965
2966
2967 if (heavyweightRequests.size() == 0) {
2968 manager.clearMarkers();
2969 }
2970 }
2971 }
2972
2973 private static boolean focusedWindowChanged(Component to, Component from) {
2974 Window wto = SunToolkit.getContainingWindow(to);
2975 Window wfrom = SunToolkit.getContainingWindow(from);
2976 if (wto == null && wfrom == null) {
2977 return true;
2978 }
2979 if (wto == null) {
2980 return true;
2981 }
2982 if (wfrom == null) {
2983 return true;
2984 }
2985 return (wto != wfrom);
2986 }
2987
2988 private static boolean isTemporary(Component to, Component from) {
2989 Window wto = SunToolkit.getContainingWindow(to);
2990 Window wfrom = SunToolkit.getContainingWindow(from);
2991 if (wto == null && wfrom == null) {
2992 return false;
2993 }
2994 if (wto == null) {
2995 return true;
2996 }
2997 if (wfrom == null) {
2998 return false;
2999 }
3000 return (wto != wfrom);
3001 }
3002
3003 static Component getHeavyweight(Component comp) {
3004 if (comp == null || comp.getPeer() == null) {
3005 return null;
3006 } else if (comp.getPeer() instanceof LightweightPeer) {
3007 return comp.getNativeContainer();
3008 } else {
3009 return comp;
3010 }
3011 }
3012
3013 static Field proxyActive;
3014
3015 private static boolean isProxyActiveImpl(KeyEvent e) {
3016 if (proxyActive == null) {
3017 proxyActive = (Field) AccessController.doPrivileged(new PrivilegedAction() {
3018 public Object run() {
3019 Field field = null;
3020 try {
3021 field = KeyEvent.class.getDeclaredField("isProxyActive");
3022 if (field != null) {
3023 field.setAccessible(true);
3024 }
3025 } catch (NoSuchFieldException nsf) {
3026 assert(false);
3027 }
3028 return field;
3029 }
3030 });
3031 }
3032
3033 try {
3034 return proxyActive.getBoolean(e);
3035 } catch (IllegalAccessException iae) {
3036 assert(false);
3037 }
3038 return false;
3039 }
3040
3041
3042 static boolean isProxyActive(KeyEvent e) {
3043 if (!GraphicsEnvironment.isHeadless()) {
3044 return isProxyActiveImpl(e);
3045 } else {
3046 return false;
3047 }
3048 }
3049
3050 private static HeavyweightFocusRequest getLastHWRequest() {
3051 synchronized(heavyweightRequests) {
3052 return (heavyweightRequests.size() > 0)
3053 ? heavyweightRequests.getLast()
3054 : null;
3055 }
3056 }
3057
3058 private static HeavyweightFocusRequest getFirstHWRequest() {
3059 synchronized(heavyweightRequests) {
3060 return (heavyweightRequests.size() > 0)
3061 ? heavyweightRequests.getFirst()
3062 : null;
3063 }
3064 }
3065 }